<template>
  <div>
    <el-dialog title="添加规格参数" :visible.sync="dialogVisible" width="60%">
      <!-- 树结构 -->
      <Tree @sendTreeData="getTreeData" />
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button
          type="primary"
          @click="innerVisible = true"
          :disabled="isDisabled"
          >确定并添加分组</el-button
        >
      </span>
      <!-- 内层弹窗 -->
      <el-dialog
        width="50%"
        title="商品规格参数配置"
        :visible.sync="innerVisible"
        append-to-body
        :before-close="clearInner"
      >
        <div class="title">当前选中的商品：{{ treeData.name }}</div>
        <div class="btn-add">
          <el-button @click="addDomain">新增规格列表</el-button>
        </div>

        <!-- 外层表单 -->
        <el-form
          :model="dyForm"
          ref="dyForm"
          label-width="100px"
          class="demo-dynamic"
        >
          <el-form-item
            v-for="(domain, index) in dyForm.domains"
            :label="domain.title"
            :key="index"
            :prop="domain.title"
            :rules="{
              required: true,
              message: '不能为空',
              trigger: 'blur',
            }"
          >
            <div class="content-item">
              <el-input v-model="domain.title"></el-input
              ><el-button @click.prevent="addChildDomain(index)" class="del-btn"
                >添加子项</el-button
              >
              <el-button @click.prevent="removeDomain(index)" class="del-btn"
                >删除</el-button
              >
            </div>

            <!-- 内层子表单 -->
            <el-form-item
              v-for="(item, index2) in domain.children"
              :label="item.title"
              :key="index2"
              :prop="item.title"
            >
              <div class="content-item child">
                <el-input v-model="item.title"></el-input>
                <el-button
                  @click.prevent="removeChildDomain(index, index2)"
                  class="del-btn"
                  >删除子项</el-button
                >
              </div>
            </el-form-item>
          </el-form-item>
        </el-form>
        <span slot="footer" class="dialog-footer">
          <el-button @click="clearInner">取 消</el-button>
          <el-button type="primary" @click="submitForm">确 定</el-button>
        </span>
      </el-dialog>
    </el-dialog>
  </div>
</template>

<script>
import Tree from "@/components/Tree";
import { addParams } from "@/api";
export default {
  components: {
    Tree,
  },
  data() {
    return {
      dialogVisible: false,
      innerVisible: false,
      isDisabled: true,
      treeData: {},
      dyForm: {
        domains: [
          {
            title: "",
            value: "",
            children: [
              {
                title: "",
                value: "",
              },
            ],
          },
        ],
      },
    };
  },
  methods: {
    getTreeData(data) {
      this.isDisabled = false;
      this.treeData = data;
    },
    // 提交表单
    submitForm() {
      this.$refs.dyForm.validate(async (valid) => {
        if (valid) {
          let obj = {
            itemCatId: this.treeData.cid,
            content: JSON.stringify(this.dyForm.domains),
          };
          let res = await addParams(obj);
          if (res.status === 200) {
            this.$message({
              type: "success",
              message: "添加成功",
            });
            this.innerVisible = false;
            this.dialogVisible = false;
            // 清空
            this.dyForm.domains = [
              {
                title: "",
                value: "",
                children: [
                  {
                    title: "",
                    value: "",
                  },
                ],
              },
            ];
            // 刷新
            this.$parent.getParamsData(1);
          }
        } else {
          this.$message({
            type: "error",
            message: "添加失败",
          });
          return false;
        }
      });
    },
    // 取消表单提交
    clearInner() {
      this.innerVisible = false;
      this.dyForm.domains = [
        {
          title: "",
          value: "",
          children: [
            {
              title: "",
              value: "",
            },
          ],
        },
      ];
    },
    resetForm() {
      this.$refs.dyForm.resetFields();
    },
    removeDomain(index) {
      this.dyForm.domains.splice(index, 1);
    },
    addDomain() {
      this.dyForm.domains.push({
        title: "",
        value: "",
        children: [],
      });
    },
    addChildDomain(index) {
      this.dyForm.domains[index].children.push({
        title: "",
        value: "",
      });
    },
    removeChildDomain(index, index2) {
      this.dyForm.domains[index].children.splice(index2, 1);
    },
  },
};
</script>

<style lang="less" scoped>
.btn-add {
  margin: 10px 0;
  padding-bottom: 10px;
  border-bottom: 1px solid lightgray;
}

.content-item {
  display: flex;
  .del-btn {
    margin-left: 10px;
  }
}

.child {
  margin: 10px 0;
}
</style>
